package com.xinke.module.system.service.logger;

import com.xinke.component.common.util.sno.IdGen;
import com.xinke.module.system.api.logger.dto.LoginLogCreateReqDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.logger.*;
import com.xinke.module.system.model.entity.logger.LoginLogDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.logger.LoginLogMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 系统访问记录 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class LoginLogServiceImpl implements LoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;

    @Override
    public Long createLoginLog(LoginLogSaveReqVO createReqVO) {
        // 插入
        LoginLogDO loginLog = BeanUtils.toBean(createReqVO, LoginLogDO.class);
        loginLogMapper.insert(loginLog);
        // 返回
        return loginLog.getLogId();
    }

    @Override
    public void createLoginLog(LoginLogCreateReqDTO reqDTO) {
        LoginLogDO loginLog = BeanUtils.toBean(reqDTO, LoginLogDO.class);
        loginLog.setLogId(IdGen.nextId());
        loginLogMapper.insert(loginLog);
    }
    @Override
    public void updateLoginLog(LoginLogSaveReqVO updateReqVO) {
        // 校验存在
        validateLoginLogExists(updateReqVO.getLogId());
        // 更新
        LoginLogDO updateObj = BeanUtils.toBean(updateReqVO, LoginLogDO.class);
        loginLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteLoginLog(Long logId) {
        // 校验存在
        validateLoginLogExists(logId);
        // 删除
        loginLogMapper.deleteById(logId);
    }

    private void validateLoginLogExists(Long logId) {
        if (loginLogMapper.selectById(logId) == null) {
            throw exception(null);
        }
    }

    @Override
    public LoginLogDO getLoginLog(Long logId) {
        return loginLogMapper.selectById(logId);
    }

    @Override
    public PageResult<LoginLogDO> getLoginLogPage(LoginLogPageReqVO pageReqVO) {
        return loginLogMapper.selectPage(pageReqVO);
    }

}